/**
  ******************************************************************************
  * @file    ProjectBsp/Wdg.c 
  * @author  zlf
  * @version V1.0.0
  * @date    2016-06-07
  * @brief   Wdg.c module
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/

	#include "wdg.h"
	
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define UWLSIFREQ 	( 60000 )
/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
static vu32 uwLsiFreq = 0;

/* Private function prototypes -----------------------------------------------*/
static void WDG_Init( void );
	
/* Private functions ---------------------------------------------------------*/
F_WDG_Typedef fWdg = 
{
	.init = WDG_Init
};

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  This function to initialise wdg.
  * @param  None
  * @retval None
  */
static void WDG_Init( void )
{


	// Check if the system has resumed from IWDG reset 
	if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)
	{
		// IWDGRST flag set
		LED_ASTP_ON;
		// Clear reset flags
		RCC_ClearFlag();
	}
	else
	{
		LED_ASTP_OFF;
	}
	
	// Get the LSI frequency:  TIM5 is used to measure the LSI frequency */
	//uwLsiFreq = GetLSIFrequency();
	// IWDG timeout equal to 250 ms 
	// the timeout may varies due to LSI frequency dispersion
	// Enable write access to IWDG_PR and IWDG_RLR registers
	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

	// IWDG counter clock: LSI/32 
	IWDG_SetPrescaler(IWDG_Prescaler_32);

	// Set counter reload value to obtain 500ms IWDG TimeOut.
	// IWDG counter clock Frequency = LsiFreq/32
	// Counter Reload Value = 500ms/IWDG counter clock period
	//                      = 0.5s / (32/LsiFreq)
	//                      = LsiFreq/(32 * 2)
	//                      = LsiFreq/64
   
	IWDG_SetReload(UWLSIFREQ/64);

	// Reload IWDG counter
	IWDG_ReloadCounter();

	// Enable IWDG - the LSI oscillator will be enabled by hardware
	IWDG_Enable();

}




